/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Bophan;
import bc.entity.Capbac;
import bc.entity.Congty;
import bc.entity.Hoadon;
import bc.entity.Lienhe;
import bc.entity.Nganh;
import bc.entity.Quanhuyen;
import bc.entity.User;
import bc.util.HibernateUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Welcome
 */
public class LienheDAO {

    private Session session;

    public LienheDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        // this.session = factory.getCurrentSession();
        this.session = factory.openSession();
    }

    public void delete(int id) {
        try {
            session.getTransaction().begin();
            session.delete(session.get(Lienhe.class, id));

            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public boolean xoa(int id) {
        boolean result = false;
        try {
            session.getTransaction().begin();
            session.delete(session.get(Lienhe.class, id));
            session.flush();
            //  session.getTransaction().commit();
            LienheDAO lhdao = new LienheDAO();
            Lienhe lh = lhdao.getLienHe1(id);
            if (lh == null) {
                result = true;
            } else {
                result = false;
            }

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return result;

    }

    public void Save(Lienhe A) {
        try {
            session.getTransaction().begin();
            session.save(A);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public Lienhe[] getAllLienHe() {
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe ORDER BY idLienhe DESC";
            Query query = session.createQuery(sql);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            session.flush();
            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe getLienHe1(int id_lienhe) {
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe where id_lienhe = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_lienhe);
            Lienhe result = (Lienhe) query.uniqueResult();
            session.flush();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe[] getLienHe(int id_user) {
        try {
            // session.getTransaction().begin();
            String sql = "from Lienhe where user=? ORDER BY id_lienhe DESC";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_user);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            //session.flush();
            // session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
//            if (session.getTransaction().isActive()) {
//                session.getTransaction().rollback();
//            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe[] getLienhebyQuan(int id) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where idquanhuyen =?";
        try {
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            session.getTransaction().commit();
            return lienheList;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return null;
    }

    public String getTenCongty(int id_congty) {
        try {
            // session.getTransaction().begin();
            String sql = "from Congty where id_congty = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_congty);
            Congty result = (Congty) query.uniqueResult();
            String congty = result.getCongty();
            // session.flush();
            // session.getTransaction().commit();
            return congty;
        } catch (Exception e) {
//            if(session.getTransaction().isActive())
//                session.getTransaction().rollback();
            e.printStackTrace();
        }
        return "";
    }

    public Hoadon[] getHoadonLH(int id_user) {
        session.getTransaction().begin();
        String sql = "from Hoadon where id_user= ?";
        try {

            Query query = session.createQuery(sql);
            query.setInteger(0, id_user);
            List result = query.list();
            Hoadon[] hoadonList = new Hoadon[result.size()];
            result.toArray(hoadonList);
            session.flush();
            session.getTransaction().commit();
            return hoadonList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    //cua nga
    public List<Lienhe> getLienheLikeName(String name) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where ten like ?";
        try {
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List result = query.list();
          //  Lienhe[] lienheList = new Lienhe[result.size()];
            //   result.toArray(lienheList);
            session.flush();
            // session.getTransaction().commit();
            return result;
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return null;
    }
    // cuaNga

    public List<Lienhe> getLienheByCapbac(Capbac cb) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where capbac =?";
        try {
            Query query = session.createQuery(sql);
            query.setEntity(0, cb);
            List result = query.list();
           // Lienhe[] arrlh = new Lienhe[result.size()];
            //  result.toArray(arrlh);
            session.flush();
            //  session.getTransaction().commit();
            return result;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByQuanhuyen(Quanhuyen qh) {
        session.getTransaction().begin();
        String sql = "from Lienhe where quanhuyen= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, qh);
            List result = query.list();
          //  Lienhe[] arrlh = new Lienhe[result.size()];
            //   result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByCongty(Congty ct) {

        session.getTransaction().begin();
        String sql = "from Lienhe where congty= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, ct);
            List result = query.list();
           // Lienhe[] arrlh = new Lienhe[result.size()];
            // result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
//cua nga

    public List<Lienhe> getLienheByBophan(Bophan bp) {
        session.getTransaction().begin();
        String sql = "from Lienhe where bophan= ?";
        try {

            Query query = session.createQuery(sql);
            query.setEntity(0, bp);
            List result = query.list();
          //  Lienhe[] arrlh = new Lienhe[result.size()];
            //   result.toArray(arrlh);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Lienhe[] getLienHeTheoTrang(int id_user, int start, int perpage) {
        try {
            session.getTransaction().begin();
            String sql= "FROM Lienhe where id_user=?";
            Query query= session.createQuery(sql);
            query.setInteger(0, id_user);
            query.setFirstResult(start);
            query.setMaxResults(perpage);
           
           
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
//            session.flush();
//            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
//            if (session.getTransaction().isActive()) {
//                session.getTransaction().rollback();
//            }
            e.printStackTrace();
        }
        return null;
    }
public Lienhe[] getAllLienHe1(){
        try {
            session.getTransaction().begin();
            String sql = "from Lienhe ORDER BY idLienhe DESC LIMIT 0,5";
            Query query = session.createQuery(sql);
            List result = query.list();
            Lienhe[] lienheList = new Lienhe[result.size()];
            result.toArray(lienheList);
            session.flush();
            session.getTransaction().commit();
            return lienheList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {
        LienheDAO a = new LienheDAO();
        //System.out.println(a.getTenCongty(1));
//          Lienhe[] b=a.getLienHe(1);
//    for(int i=0;i<b.length;i++){
//         int id_congty=b[i].getCongty().getIdCongty();
//          System.out.println(a.getTenCongty(id_congty)) ;
        //System.out.println(id_congty);
        // }
        Lienhe[] b = a.getAllLienHe1();
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i].getTen());
        }
    }
}
